#!/usr/bin/env python
import os
import subprocess
import sys

def convert_imx_to_zxm(search_root, script_dir):
    for dirpath, _, filenames in os.walk(search_root):
        for filename in filenames:
            if filename.lower().endswith('.imx'):
                imx_path = os.path.join(dirpath, filename)
                zxm_path = os.path.splitext(imx_path)[0] + '.zxm'
                try:
                    print(f"Converting: {imx_path} -> {zxm_path}")
                    subprocess.run(
                        ['python', os.path.join(script_dir, 'imx2zxm.py'), imx_path, zxm_path],
                        check=True
                    )
                except subprocess.CalledProcessError as e:
                    print(f"Failed to convert {imx_path}: {e}")

if __name__ == '__main__':
    # 当前工作目录（用户运行脚本时所在的目录）
    work_dir = os.getcwd()

    # 本脚本所在目录（imx2zxm.py 所在处）
    script_dir = os.path.abspath(os.path.dirname(__file__))

    convert_imx_to_zxm(work_dir, script_dir)
